package com.java.star.mall.front.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.java.star.mall.common.response.JsonResult;
import com.java.star.mall.front.pojo.param.AddCartParam;
import com.java.star.mall.front.pojo.param.UpdateCartCheckedParam;
import com.java.star.mall.front.pojo.param.UpdateCartNumParam;
import com.java.star.mall.front.pojo.vo.CartVO;
import com.java.star.mall.front.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 购物车Controller
 */
@Api(value = "cart", tags = "购物车")
@RestController
@RequestMapping("/cart")
@Validated
public class CartController {

    @Autowired
    private CartService cartService;

    @ApiOperation(value = "查询购物车")
    @ApiOperationSupport(order = 100)
    @GetMapping("/listCart")
    public JsonResult<List<CartVO>> listCart() {
        List<CartVO> cartVOList = cartService.listCart();
        return JsonResult.ok(cartVOList);
    }

    @ApiOperation(value = "添加购物车商品")
    @ApiOperationSupport(order = 200)
    @PostMapping("/addToCart")
    public JsonResult<Void> addToCart(@Valid @RequestBody AddCartParam addCartParam) {
        cartService.addToCart(addCartParam);
        return JsonResult.ok();
    }

    @ApiOperation(value = "批量修改购物车的选中状态")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "checked", value = "选中状态", required = true, dataType = "Boolean")
    })
    @PostMapping("/batchChangeChecked/{checked}")
    public JsonResult<Void> batchChangeChecked(@PathVariable Boolean checked) {
        cartService.batchChangeChecked(checked);
        return JsonResult.ok();
    }

    @ApiOperation(value = "修改购物车购物项的选中状态")
    @ApiOperationSupport(order = 400)
    @PostMapping("/changeChecked")
    public JsonResult<Void> changeChecked(@Valid @RequestBody UpdateCartCheckedParam updateCartCheckedParam){
        cartService.changeChecked(updateCartCheckedParam);
        return JsonResult.ok();
    }

    @ApiOperation(value = "修改购物项数量")
    @ApiOperationSupport(order = 500)
    @PostMapping("/changeNum")
    public JsonResult<Void> changeNum(@Valid @RequestBody UpdateCartNumParam updateCartNumParam) {
        cartService.changeNum(updateCartNumParam);
        return JsonResult.ok();
    }

    @ApiOperation(value = "删除购物车商品")
    @ApiOperationSupport(order = 600)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value="商品id", required = true, dataType = "Integer")
    })
    @PostMapping("/deleteToCart/{productId}")
    public JsonResult<Void> deleteToCart(@PathVariable Integer productId) {
        cartService.deleteToCart(productId);
        return JsonResult.ok();
    }
    
}
